package com.gking.aiService.serviceUtils.RemoteInteraction;

import com.gking.aiService.common.BaseContext;
import com.gking.aiService.common.Result;
import com.gking.aiService.common.ResultResponse;
import com.gking.aiService.realm.MyRealm;
import com.gking.aiService.realm.MyRealmTwo;
import com.gking.aiService.utils.GetAuthInfoUtil;
import com.gking.aiService.utils.RestTemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


@Slf4j
@Service
@Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS) //三级缓存解决bean循环注入问题
public class RemoteInteractionServiceImpl implements RemoteInteractionService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private MyRealm myRealm;

    @Autowired
    private MyRealmTwo myRealmTwo;

    @Resource
    private HttpServletRequest request;


    /**
     * 获取用户角色详情
     *
     * @return s
     */
    @Override
    public ResultResponse getUserRolesDetail() {
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", request.getHeader("Authorization"));
        headers.put(HttpHeaders.COOKIE, request.getHeader(HttpHeaders.COOKIE));
        headers.put("c-device-id", request.getHeader("c-device-id"));

        HttpEntity<String> stringHttpEntity = RestTemplateUtil.httpGET(
                "/userRoles/limit/getUserRoles/" + BaseContext.getCurrentId(),
                "", headers);

        return RestTemplateUtil.parseJSON(stringHttpEntity.getBody());
    }


    /**
     * 获取角色权限详情
     *
     * @param roleId 角色id
     * @return s
     */
    @Override
    public ResultResponse getRolePermissionsDetail(String roleId) {
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", request.getHeader("Authorization"));
        headers.put(HttpHeaders.COOKIE, request.getHeader(HttpHeaders.COOKIE));
        headers.put("c-device-id", request.getHeader("c-device-id"));

        HttpEntity<String> stringHttpEntity = RestTemplateUtil.httpGET(
                "/rolePermissions/limit/rolePermissionsDetail/" + roleId,
                "", headers);

        return RestTemplateUtil.parseJSON(stringHttpEntity.getBody());
    }

    /**
     * 退出登录
     *
     * @return s
     */
    @Override
    public Result<String> logout() {
        // 删除角色、权限缓存
        myRealm.clearCached();
        myRealmTwo.clearCached();
        return Result.success("退出登录成功");
    }


    /**
     * 获取用户登录缓存的凭证信息
     *
     * @return s
     */
    @Override
    public Result<Map> getUserLoginAuth(HttpServletRequest request) {
        try {
            Map<String, String> headers = new HashMap<>();
            headers.put("Authorization", request.getHeader("Authorization"));
            headers.put("c-device-id", request.getHeader("c-device-id"));
            String paramStr = "?ip=" + GetAuthInfoUtil.getClientIp(request);
            HttpEntity<String> stringHttpEntity = RestTemplateUtil.httpGET("/user/limit/loginAuth", paramStr, headers);
            ResultResponse resultResponse = RestTemplateUtil.parseJSON(stringHttpEntity.getBody());
            assert resultResponse != null;
            log.info("resultResponse : {} ", resultResponse);
            log.info("resultResponse : {} ", resultResponse.getData());
            return Result.success("获取用户登录缓存的凭证信息成功", (Map) resultResponse.getData());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.success("获取用户登录缓存的凭证信息为空", null);
        }
    }
}
